package com.bawei.image

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.BlurTransformation

/**
 * 单例的图片加载器
 */
object GlideLoader {

    fun loadImage(ctx: Context, url: String, iv: ImageView){
        Glide.with(ctx)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .transform(CenterCrop())
            .into(iv)
    }

    /**
     * 加载圆形图片
     */
    fun loadCircleImage(context: Context, url: String, imageView: ImageView) {
        Glide.with(context)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .error(ColorDrawable(Color.YELLOW))
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.ALL) //磁盘缓存策略：NONE, ALL，DATA，RESOURCE，AUTOMATIC
            .transform(CenterCrop(), CircleCrop())
            .into(imageView)
    }

    /**
     * 加载圆角图片
     */
    fun loadBoundImage(context: Context, url: String, radio: Int, imageView: ImageView) {
        Glide.with(context)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .error(ColorDrawable(Color.YELLOW))
            .skipMemoryCache(true)
            .transform(CenterCrop(), RoundedCorners(radio))
            .into(imageView)
    }

    /**
     * 加载本地资源图片
     */
    fun loadResImage(view: View, resId: Int, imageView: ImageView) {
        Glide.with(view)
            .load(resId)
            .skipMemoryCache(true)
            .transform(CenterCrop())
            .into(imageView)
    }


    /**
     * 旋转图片
     */
    fun loadRotateImage(view: View, url: String, deg: Int, imageView: ImageView) {
        Glide.with(view)
            .load(url)
            .skipMemoryCache(true)
            .transform(RotateTransformation(deg))
            .into(imageView)
    }

    /**
     * 加载毛玻璃效果图片
     */
    fun loadBlurCircleImage(context: Context, url: String?, iv: ImageView) {
        //定义图片转换对象
        val options: MultiTransformation<Bitmap> = MultiTransformation(
            BlurTransformation(),
            CircleCrop(),
            CenterCrop()
        )
        Glide.with(context)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .error(ColorDrawable(Color.YELLOW))
            .skipMemoryCache(true)
            .apply(RequestOptions.bitmapTransform(options))
            .into(iv)
    }

}